import { consola } from 'consola'
import { colors } from 'consola/utils'
import {
  createResolver,
  defineNuxtModule,
  addImportsDir,
  hasNuxtModule,
  addComponentsDir
} from '@nuxt/kit'
import { defu } from 'defu'

export interface ModuleOptions {
  license?: string
  routerOptions?: boolean
  content?: boolean
}

export default defineNuxtModule({
  meta: {
    name: 'modules-pro',
    configKey: 'modulesPro',
    compatibility: {
      nuxt: '^3.10.0'
    }
  },
  defaults: {
    license: '',
    routerOptions: true,
    content: false
  },
  setup(options, nuxt) {
    const resolver = createResolver(import.meta.url)

    if (options.routerOptions) {
      nuxt.hook('pages:routerOptions', ({ files }) => {
        files.push({
          path: resolver.resolve('runtime/app/router.options.ts'),
          optional: true
        })
      })
    }

    /**
     * Add Content components & utils only if Nuxt Content is present
     */
    if (hasNuxtModule('@nuxt/content') || options.content) {
      // Add auto-imported utils
      addImportsDir(resolver.resolve('runtime/utils'))

      addComponentsDir({
        path: resolver.resolve('runtime/components/global'),
        global: true,
        prefix: '',
        pathPrefix: false
      })

      addComponentsDir({
        path: resolver.resolve('runtime/components/content'),
        prefix: 'U',
        pathPrefix: false
      })

      // @ts-ignore
      nuxt.options.content = defu(nuxt.options.content, {
        highlight: {
          theme: {
            light: 'material-theme-lighter',
            default: 'material-theme',
            dark: 'material-theme-palenight'
          },
          preload: [
            'json',
            'js',
            'ts',
            'html',
            'css',
            'vue',
            'diff',
            'shell',
            'markdown',
            'yaml',
            'bash',
            'ini'
          ]
        },
        navigation: {
          fields: ['icon', 'to', 'target']
        }
      })

      // @ts-ignore
      nuxt.hook('tailwindcss:config', function (tailwindConfig) {
        tailwindConfig.content = tailwindConfig.content ?? { files: [] }
        ;(Array.isArray(tailwindConfig.content)
          ? tailwindConfig.content
          : tailwindConfig.content.files
        ).push(resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'))
      })
    }
  }
})
